package controllers

import (
	"XYZShell/mgotool/models"
	"strconv"

	"github.com/astaxie/beego"
)

type CollectionController struct {
	beego.Controller
}

func (this *CollectionController) Get() {
	dbname := this.Ctx.Input.Param(":dbname")
	c := this.Ctx.Input.Param(":c")
	page := this.Ctx.Input.Query("p")
	pagesize := this.Ctx.Input.Query("psize")
	p, _ := strconv.Atoi(page)
	psize, _ := strconv.Atoi(pagesize)
	var mgotool *models.Mgotool
	mgotool = models.GetMgotool()
	this.Data["DbName"] = dbname
	this.Data["CName"] = c
	this.Data["JsonData"] = mgotool.GetJsonData(dbname, c, p, psize)
	this.TplName = "collection.html"
}

func (this *CollectionController) Post() {
	dbname := this.Ctx.Input.Param(":dbname")
	c := this.Ctx.Input.Param(":c")
	edittype := this.Ctx.Request.FormValue("t")
	jsonstr := this.Ctx.Request.FormValue("json")
	var mgotool *models.Mgotool
	mgotool = models.GetMgotool()
	if edittype == "1" {
		mgotool.InsertCollection(dbname, c, jsonstr)
	}
	this.Data["DbName"] = dbname
	this.Data["CName"] = c
	this.Data["JsonData"] = mgotool.GetJsonData(dbname, c, 1, 15)
	this.TplName = "collection.html"
}
